﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Collections.ObjectModel;
using System.Web.Script.Serialization;

namespace Medianamik.Core.Synchronization
{
    [Serializable]
    public class Record : SynchronizedHierarchicalEntity
    {
        public Guid? NodeTypeId { get; set; }
        public String NodeTypeName{ get; set; }

        //Ajout pour le medianamikLookup 03-12-2009
        public String RecordName { 
            get
            {
                if (this["Name"] != null && this["Name"].Value != null)
                    return this["Name"].Value.ToString();
                return string.Empty;
            }
        }

        public IList<RecordProperty> Properties
        {
            get { return _properties; }
            set { _properties = value; }
        }
        IList<RecordProperty> _properties = new List<RecordProperty>();
        [ScriptIgnore]
        public IList<String> InactiveCultureNames
        {
            get { return _inactiveCultureNames; }
            set { _inactiveCultureNames = value; }
        }
        IList<String> _inactiveCultureNames = new List<String>();
        public short Position { get; set; }
        public bool IsSubEntity { get; set; }
        public bool ExistsInCurrentCulture { get; set; }
        public string ParentIdPropertyName{ get; set; }
        private string _defaultCultureName;
        public string DefaultCultureName
        {
            get { return _defaultCultureName ?? ""; }
            set { _defaultCultureName = value; }
        }

        public DateTime? CreatedOn { get; set; }
        public string CreatedBy { get; set; }
        public string UpdatedBy { get; set; }
        public DateTime? UpdatedOn { get; set; }
        public string ParentIdReferencedTypeName{ get; set; }

        public bool? IsActive { get; set; }

        public RecordProperty this[string name]
        {
            get
            {
                return Properties.FirstOrDefault(p => p.NodeTypePropertyName.Equals(name));
            }
        }

        public static int ComparePosition(Record n1, Record n2)
        {
            return n1.Position.CompareTo(n2.Position);
        }

        public HashSet<CultureInfo> DetectCultures()
        {
            var cultures = Properties.Select(p => p.CultureName).Distinct().ToList();

            if (cultures.Count > 1)
                cultures.Remove("");

            var c = new HashSet<CultureInfo>();

            cultures.ForEach(x => c.Add(CultureInfo.GetCultureInfo(x)));

            return c;
        }

        public bool IsEqualTo(Record record)
        {
            var equals = record.Properties.Count == Properties.Count;

            if(equals)
            {
                foreach (var ownProtperty in Properties)
                {
                    var property = record.Properties
                        .FirstOrDefault(p => p.NodeTypePropertyId == ownProtperty.NodeTypePropertyId);

                    if (property == null)
                        return false;

                    if ((property.Value == null && ownProtperty.Value != null) ||
                            (ownProtperty.Value == null && property.Value != null))
                        return false;

                    if (!(property.Value.ToString().EqualsIgnoreCase(ownProtperty.Value.ToString())))
                        return false;
                }
            }

            return equals;
        }

        public bool HasProperty(string propertyName)
        {
            return Properties.Any(p => p.NodeTypePropertyName.EqualsIgnoreCase(propertyName));
        }
    }
}
